package com.ebrothers.isns.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

/**
 * @sine 2011-4-19
 */
public class PrefsManager {
	// ===========================================================
	// Constants
	// ===========================================================
	public static final String PREFS_SETTINGS = "settings";
	// ===========================================================
	// Fields
	// ===========================================================
	private static PrefsManager sSingleton;
	private SharedPreferences mPreferences;
	private Editor mEditor;

	// ===========================================================
	// Constructors
	// ===========================================================
	private PrefsManager(Context context) {
		mPreferences = context.getSharedPreferences(PREFS_SETTINGS, 0);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public static synchronized PrefsManager getInstance(Context context) {
		if (sSingleton == null) {
			sSingleton = new PrefsManager(context);
		}
		return sSingleton;
	}

	public void registerOnSharedPreferenceChangeListener(
			OnSharedPreferenceChangeListener listener) {
		mPreferences.registerOnSharedPreferenceChangeListener(listener);
	}

	public void unregisterOnSharedPreferenceChangeListener(
			OnSharedPreferenceChangeListener listener) {
		mPreferences.unregisterOnSharedPreferenceChangeListener(listener);
	}

	public synchronized void edit() {
		mEditor = mPreferences.edit();
	}

	public synchronized void commit() {
		if (mEditor != null) {
			mEditor.commit();
			mEditor = null;
		}
	}

	public synchronized void putString(String key, String value) {
		if (mEditor != null) {
			mEditor.putString(key, value);
		}
	}

	public synchronized void putBoolean(String key, boolean value) {
		if (mEditor != null) {
			mEditor.putBoolean(key, value);
		}
	}

	public synchronized void putInt(String key, int value) {
		if (mEditor != null) {
			mEditor.putInt(key, value);
		}
	}

	public synchronized void putFloat(String key, float value) {
		if (mEditor != null) {
			mEditor.putFloat(key, value);
		}
	}

	public String getString(String key, String defValue) {
		return mPreferences.getString(key, defValue);
	}

	public int getInt(String key, int defValue) {
		return mPreferences.getInt(key, defValue);
	}

	public float getFloat(String key, float defValue) {
		return mPreferences.getFloat(key, defValue);
	}

	public boolean getBoolean(String key) {
		return mPreferences.getBoolean(key, false);
	}

	public boolean getBoolean(String key, boolean defValue) {
		return mPreferences.getBoolean(key, defValue);
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	public static interface Keys {
		public static final String SERVER_IP = "server_ip";
		public static final String LAST_LOCATION_NAME = "last_location_name";
		public static final String LAST_LOCATION_LONGITUDE = "last_location_longitude";
		public static final String LAST_LOCATION_LATITUDE = "last_location_latitude";
	}

}
